package com.IClass.IActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.IClass.IClassApi;
import com.IClass.IGridViewListAdapter;
import com.IClass.ImageUtil;
import com.IClass.NetUploadPictureListen;
import com.IClass.NetUtil;
import com.IClass.App.IClass;
import com.IClass.App.R;
import com.IClass.Server.AppJsonDefine.GenJavaCode.AppHttpHelp;
import com.IClass.Server.AppJsonDefine.GenJavaCode.homeblogcomment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;



public class ILookActivity extends Activity  {

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// FIXME Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(data != null)
		{
			
			AlertDialog.Builder ad=new AlertDialog.Builder(ILookActivity.this);
			/*
			setTitle()：给对话框设置title.
			setIcon():给对话框设置图标。
			setMessage():设置对话框的提示信息
			setItems()：设置对话框要显示的一个list,一般用于要显示几个命令时
			setSingleChoiceItems():设置对话框显示一个单选的List
			setMultiChoiceItems():用来设置对话框显示一系列的复选框。
			setPositiveButton():给对话框添加”Yes”按钮。
			setNegativeButton():给对话框添加”No”按钮。
			show():显示对话框，一般放最后
			*/
			final Uri uri = data.getData(); 
			final ContentResolver cr = this.getContentResolver();  
			
			ad.setTitle("上传");// 设置对话框标题
			ad.setMessage("是否确认上传");// 设置对话框内容
			ad.setPositiveButton("确认", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int i) {
					// TODO Auto-generated method stub
					Bitmap cameraBitmap = null;
		            try {  
		            	cameraBitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));  
		                
		                  
		            } catch (FileNotFoundException e) {  
		            	
		            }  
		            
					 //(Bitmap) data.getExtras().get("data");

					//selectimage.setImageBitmap(cameraBitmap);

					strImgPath = Environment.getExternalStorageDirectory().toString();
					strImgPath += "/ioclass/imagescach/bbstextselectimage.png";

					ImageUtil.saveBitmap(cameraBitmap, strImgPath);
					
					progressDialog = ProgressDialog.show(ILookActivity.this, "请稍等...","上传图片中...", true);
					new Thread() {
						@Override
						public void run() 
						{
							// FIXME Auto-generated method stub
							NetUtil.UpLoadImage(IClassApi.SERVER_HOST+ AppHttpHelp.IdentificationPicture(IClass.getUserid()),strImgPath, new NetUploadPictureListen() 
								{

											@Override
											public void OnResult(String result) 
											{
												// FIXME Auto-generated method stub
												Editor sharedata = getSharedPreferences("IClass", MODE_PRIVATE).edit();
												sharedata.putString("GetUploadlookpicture", "");
												sharedata.commit();
												IClass.getPictureresult().clear();

											}

								}
							);
							
							mhandle.post(new Runnable() 
							{
								@Override
								public void run() 
								{
									progressDialog.dismiss();
									Toast.makeText(ILookActivity.this, "上传图片成功", Toast.LENGTH_LONG).show();
								}

							});
							
						}

					}.start();
				}
			});
			ad.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int i) {
					
				}
			});
			ad.show();// 显示对话框
					
			
		}
	}
	public Button camerabtn;
	public Button regetbtn;
	public Button postbtn;
	public Button repeatbtn;
	public Button picbtn;
	public Context context;
	public GridView gridview;
	public WebView commentview;
	
	public Handler mhandle = new Handler();
	public ProgressDialog progressDialog;
	
	public String strImgPath;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// FIXME Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.ilook_funsview_layout);
		
		camerabtn = ((Button)findViewById(R.id.camerabtn));
		regetbtn = ((Button)findViewById(R.id.regetbtn));
		postbtn = ((Button)findViewById(R.id.postbtn));
		repeatbtn = ((Button)findViewById(R.id.repeatbtn));
		picbtn = ((Button)findViewById(R.id.picbtn));
		
		gridview = ((GridView)findViewById(R.id.gridView1));
//		List<String> buttontextlist = new ArrayList<String>();
//		buttontextlist.add("拍照");
//		buttontextlist.add("照片");
//		IGridViewListAdapter list = new IGridViewListAdapter(this,buttontextlist);
		
		List<Map<String, Object>> items = new ArrayList<Map<String,Object>>();
        
        Map<String, Object> item = new HashMap<String, Object>();
        item.put("imageItem", R.drawable.camera);
        item.put("textItem", "拍照");
        items.add(item);
        
        item = new HashMap<String, Object>();
        item.put("imageItem", R.drawable.pictures);
        item.put("textItem", "图贴");
        items.add(item);
        
        item = new HashMap<String, Object>();
        item.put("imageItem", R.drawable.uploadpic);
        item.put("textItem", "贴问");
        items.add(item);
        
        item = new HashMap<String, Object>();
        item.put("imageItem", R.drawable.ask);
        item.put("textItem", "发贴");
        items.add(item);
        
//实例化一个适配器
        SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.grid_item, new String[]{"imageItem", "textItem"}, new int[]{R.id.image_item, R.id.text_item});
        
		gridview.setAdapter(adapter);
		
		gridview.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// FIXME Auto-generated method stub
				if(arg2 == 0)
				{
					Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //调用照相机
					//intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
					
					String strImgPath = Environment.getExternalStorageDirectory().toString() + "/ioclass/images/";// 存放照片的文件夹
			        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";// 照片命名
			        File out = new File(strImgPath);
			        if (!out.exists()) {
			            out.mkdirs();
			        }
			        out = new File(strImgPath, fileName);
			        strImgPath = strImgPath + fileName;// 该照片的绝对路径
			        Uri uri = Uri.fromFile(out);
			        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
			        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);  
					startActivityForResult(intent, 1);
				}
				else if(arg2 == 1)
				{
					SharedPreferences  share = context.getSharedPreferences("IClass", MODE_PRIVATE);  
			        String userid = share.getString("userid", "0");
			        
			        if(userid == "0")
			        {
						Intent mainIntent = new Intent(ILookActivity.this,IRegisterActivity.class); 
		                startActivity(mainIntent);    //启动Main界面
		                
			        	
			        }
			        else
			        {
			        	IClass.setUserid(userid);
			        	

						//Intent intent = new Intent(context, ILookPicturesSwitchViewActivity.class); //查看照片
						//startActivity(intent);
						
						Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
					    intent.addCategory(Intent.CATEGORY_OPENABLE);
					    intent.setType("image/*");
					    
//					    intent.putExtra("crop", "true");
//					    intent.putExtra("aspectX", 1);
//					    intent.putExtra("aspectY", 1);
//					    intent.putExtra("outputX", 80);
//					    intent.putExtra("outputY", 80);
					    //intent.putExtra("return-data", true);

					    startActivityForResult(intent, 1);
			        	
			        	// FIXME Auto-generated method stub

						
		        			
		        			
		        		
			        	
			        }
			        
				    
				}
				else if(arg2 == 2)
				{
					Intent intent = new Intent(context, ILookUploadpictureActivity.class); //贴板					
					startActivity(intent);

				}
				else
				{
					Intent intent = new Intent(context, ILookAskActivity.class); //发帖					
					startActivity(intent);
					
				}
			}
			
		});
		
		context = this;
//		camerabtn.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View v) {
//				// FIXME Auto-generated method stub
//				Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //调用照相机
//				//intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
//				
//				String strImgPath = Environment.getExternalStorageDirectory().toString() + "/ioclass/images/";// 存放照片的文件夹
//		        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";// 照片命名
//		        File out = new File(strImgPath);
//		        if (!out.exists()) {
//		            out.mkdirs();
//		        }
//		        out = new File(strImgPath, fileName);
//		        strImgPath = strImgPath + fileName;// 该照片的绝对路径
//		        Uri uri = Uri.fromFile(out);
//		        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//		        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);  
//				startActivityForResult(intent, 1);
//				
//			}
//			
//		});
		
//		regetbtn.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View v) {
//				// FIXME Auto-generated method stub
//				
//			}
//			
//		});
//		
//		postbtn.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View v) {
//				// FIXME Auto-generated method stub
//				  
//				
//			}
//			
//		});
//		
//		picbtn.setOnClickListener(new OnClickListener(){
//
//			@Override
//			public void onClick(View v) {
//				// FIXME Auto-generated method stub
//				Intent intent = new Intent(context, ILookPicturesSwitchViewActivity.class); //调用照相机
//				
//				startActivity(intent);
//				
//			}
//			
//		});
	}
	
}
